layui.use(['table', 'admin', 'ax','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;

    /**
     * 档案借阅信息表管理
     */
    var ArchiveBorrow = {
        tableId: "archiveBorrowTable"
    };

    /**
     * 初始化表格的列
     */
    ArchiveBorrow.initColumn = function () {
        return [[
            {type: 'radio', fixed: 'left', width: 80},
            {field: 'id', hide: true, title: 'id'},
            {field: 'djh', sort: true, title: '借阅单号'},
            {field: 'jyrmc', title: '借阅人'},
            {field: 'jysj', title: '借阅时间'},
            {field: 'sfgh', width:100,title: '是否归还',templet:'#sfgh'},
            {field: 'beiz', title: '备注'},
            {fixed: 'right', align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    ArchiveBorrow.search = function () {
        var condi = $.extend({},form.val('searchForm'));
        table.reload(ArchiveBorrow.tableId, {
            where: condi, page: {curr: 1}
        });
    };

    //表单提交事件
    form.on('submit(btnSearch)', function (data) {
        ArchiveBorrow.search();
        return false;
    });


    /**
     * 弹出添加对话框
     */
    ArchiveBorrow.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/archiveBorrow/add';
    };

    /**
     * 导出excel按钮
     */
    ArchiveBorrow.exportExcel = function () {
        var checkRows = table.checkStatus(ArchiveBorrow.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ArchiveBorrow.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/archiveBorrow/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ArchiveBorrow.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/archiveBorrow/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(ArchiveBorrow.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    table.render({
        elem: '#' + ArchiveBorrow.tableId,
        url: Feng.ctxPath + '/archiveBorrow/list',
        page: true,
        height: "500",
        cellMinWidth: 100,
        cols: ArchiveBorrow.initColumn()
    });

    table.on('row(' + ArchiveBorrow.tableId + ')', function (obj) {
        ArchiveInfo.reloadArchive(obj.data.id);
    })


    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ArchiveBorrow.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ArchiveBorrow.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ArchiveBorrow.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'delete') {
            ArchiveBorrow.onDeleteItem(data);
        }
    });


    //借阅的档案列表
    /**
     * 档案信息表管理
     */
    var ArchiveInfo = {
        tableId: "archiveBorrowArchiveTable"
    };

    /**
     * 初始化表格的列
     */
    ArchiveInfo.initColumn = function () {
        return [[
            {type: 'checkbox', fixed: 'left', width: 80},
            {field: 'wjid', sort: true, title: '卷宗号'},
            {field: 'mc', width: 120, title: '档案名称'},
            {field: 'ssrrmc', title: '所属人'},
            {field: 'sfgh',width:100,title:'是否归还',templet:'#sfgh'},
            {fixed: 'right',align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    // 渲染表格
    table.render({
        elem: '#' + ArchiveInfo.tableId,
        data: [],
        page: false,
        height: "558",
        cellMinWidth: 100,
        cols: ArchiveInfo.initColumn()
    });

    ArchiveInfo.reloadArchive = function (parentId) {
        table.reload(ArchiveInfo.tableId, {
            url: Feng.ctxPath + '/archiveInfo/listBorrow?id=' + parentId,
        });
    };

    // 点击确定
    $('#batReturn').click(function () {
        //点了选中
        var status = table.checkStatus(ArchiveInfo.tableId);
        if (status.data.length == 0) {
            Feng.error("请至少选中一条数据");
            return;
        }
        var ids = [];
        status.data.forEach(function (val) {
            if(val.sfgh == '0') {
                ids.push(val.id);
            }
        });

        if(ids.length == 0) {
            Feng.info("您所选的档案都已归还");
            return;
        }

        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/archiveBorrowDetail/batUpdate", function (data) {
                Feng.success("归还成功!");
                table.reload(ArchiveInfo.tableId);
                table.reload(ArchiveBorrow.tableId);
            }, function (data) {
                Feng.error("归还失败!" + data.responseJSON.message + "!");
            });
            ajax.set("ids", ids.join(","));
            ajax.start();
        };
        Feng.confirm("是否归还所选档案?", operation);

    });

    // 工具条点击事件
    table.on('tool(' + ArchiveInfo.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'delete') {
            ArchiveInfo.onDeleteItem(data);
        }
    });


    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ArchiveInfo.onDeleteItem = function (data) {
        if (data.sfgh == '1') {
            Feng.info("改档案已归还，无法删除");
            return;
        }

        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/archiveBorrowDetail/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(ArchiveInfo.tableId);
                table.reload(ArchiveBorrow.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

});
